package com.zx.idc.backend.gui.slog;


import com.baomidou.mybatisplus.plugins.Page;
import com.zx.idc.backend.gui.common.BasicBaseApi;
import com.zx.idc.backend.gui.common.Result;
import com.zx.idc.backend.gui.slog.service.SlogIllegalSiteInterfaceService;
import com.zx.idc.common.exception.EncryptException;
import com.zx.idc.common.exception.FTPUploadFailedException;
import com.zx.idc.ds.slog.entity.HandleEntity;
import com.zx.idc.ds.slog.entity.SlogIllegalSite;
import com.zx.idc.ds.slog.service.ISlogIllegalSiteService;
import com.zx.idc.ds.slog.vo.SlogIllegalSiteVo;
import com.zx.idc.internal.exception.SendMessageException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 违法网站 前端控制器
 * </p>
 *
 * @author chendong
 * @since 2019/3/26
 */

@RestController
@RequestMapping("/api/slogIllegalSite")
public class SlogIllegalSiteController extends BasicBaseApi<ISlogIllegalSiteService, SlogIllegalSite> {

    @Autowired
    private SlogIllegalSiteInterfaceService slogIllegalSiteReportService;

    @GetMapping("/query-page")
    @RequiresPermissions("regulatory-log:slog-illegal-site")
    public Result<Page<SlogIllegalSiteVo>> page(Page<SlogIllegalSiteVo> page, SlogIllegalSiteVo slogIllegalSiteVo) {

        return success(service.pageByParam(page, slogIllegalSiteVo));
    }

    @GetMapping("/list")
    @RequiresPermissions("regulatory-log:slog-illegal-site")
    public Result<List<SlogIllegalSiteVo>> list(SlogIllegalSiteVo slogIllegalSiteVo) {
        return success(service.listByParam(slogIllegalSiteVo));
    }

    @PostMapping(value = "/report-data")
    @RequiresPermissions("regulatory-log:slog-illegal-site:edit")
    public Result<Boolean> report(@RequestBody HandleEntity<SlogIllegalSiteVo> slogIllegalSiteHandleEntity) {
        String msg;
        Exception ex;
        try {
            return success(slogIllegalSiteReportService.reportData(slogIllegalSiteHandleEntity));
        } catch (FTPUploadFailedException e) {
            msg = e.getMessage();
            ex = e;
        } catch (Exception e) {
            msg = "上报异常";
            ex = e;
        }
        log.error(msg, ex);
        return error(msg);
    }

    @PostMapping(value = "/handle")
    @RequiresPermissions("regulatory-log:slog-illegal-site:edit")
    public Result<Boolean> handle(@RequestBody HandleEntity<SlogIllegalSiteVo> handleEntity) {
        String errorMsg;
        Exception ex;
        try {
            return success(service.updateAndHandle(handleEntity));
        } catch (SendMessageException e) {
            errorMsg = "违规网站处置接口异常:" + e.getMessage();
            ex = e;
        } catch (EncryptException e) {
            log.error("违规网站处置加密异常:" + e.getMessage());
            errorMsg = "违规网站处置加密异常";
            ex = e;
        } catch (Exception e) {
            errorMsg = "违规网站处置异常:" + e.getMessage();
            ex = e;
        }
        log.error(errorMsg, ex);
        return error(errorMsg);
    }

    @PostMapping(value = "/cancel-handle")
    @RequiresPermissions("regulatory-log:slog-illegal-site:edit")
    public Result<Boolean> cancelHandle(@RequestBody HandleEntity<SlogIllegalSiteVo> handleEntity) {
        String errorMsg;
        Exception ex;
        try {
            return success(service.updateAndCancelHandle(handleEntity));
        } catch (SendMessageException e) {
            errorMsg = "违规网站取消处置接口异常";
            ex = e;
        } catch (EncryptException e) {
            errorMsg = "违规网站取消处置加密异常";
            ex = e;
        } catch (Exception e) {
            errorMsg = "违规网站处置异常";
            ex = e;
        }
        log.error(errorMsg, ex);
        return error(errorMsg);
    }

}

